package org.summerframework.component.security.spring.filter;

import org.summerframework.component.security.spring.exception.UrlAuthenticationFailureHandler;
import org.springframework.security.web.util.matcher.RequestMatcher;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.summerframework.web.filter.BaseFilter;


/**
 * @author shichao
 */
public class LoginDataFilter implements BaseFilter {

    private RequestMatcher requiresAuthenticationRequestMatcher;
    private UrlAuthenticationFailureHandler urlAuthenticationFailureHandler;

    public LoginDataFilter(RequestMatcher requiresAuthenticationRequestMatcher, UrlAuthenticationFailureHandler urlAuthenticationFailureHandler) {
        this.requiresAuthenticationRequestMatcher = requiresAuthenticationRequestMatcher;
        this.urlAuthenticationFailureHandler = urlAuthenticationFailureHandler;
    }

    @Override
    public UrlAuthenticationFailureHandler getFilterFailureHandler() {
        return urlAuthenticationFailureHandler;
    }

    @Override
    public void onFilter(HttpServletRequest request, HttpServletResponse response) {
//        if (requiresAuthenticationRequestMatcher.matches((HttpServletRequest) request)) {
//            RequestContext context = RequestContext.getContext();
//
//            if (context.getLoginData().isLogin()) {
//                LoginSecurityData loginSecurityData = new LoginSecurityData(context.getLoginData());
//                SecurityContextHolder.getContext().setAuthentication(loginSecurityData);
//            } else {
//                throw new NotLoginAuthenticationException(SecurityResultCode.ERROR_NOT_LOGIN.getMsg());
//            }
//        }
    }
}
